// src/strategy/jwt.strategy.ts

import { CustomStrategy, PassportStrategy } from '@midwayjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { Config } from '@midwayjs/core';
import { JwtConfigInterface } from '../interface';

@CustomStrategy()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
  /**
   * jwt 配置
   */
  @Config('jwt')
  jwtConfig: JwtConfigInterface;

  /**
   * token解析验证
   * @param payload
   * @returns
   */
  async validate(payload) {
    return payload;
  }

  /**
   * 获取jwt配置信息
   * @returns
   */
  getStrategyOptions(): any {
    return {
      secretOrKey: this.jwtConfig.secret,
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
    };
  }
}
